/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package br.com.menosehmais.db;

import br.com.menosehmais.framework.configuration.PersistenceUnit;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.persistence.EntityManager;
import org.dbunit.DatabaseUnitException;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.ReplacementDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
import org.dbunit.operation.DatabaseOperation;
import org.joda.time.LocalDateTime;

/**
 *
 * @author RafaelRosa
 */
public class CargaDeBanco {
    public static final int NUMERO_MAXIMO = 1000;
    private static final double QTD_DIAS_NO_ANO = 365.2425;
    protected static EntityManager em;
    protected IDatabaseConnection connection;
    //private static final EntityManagerFactory emf = Persistence.createEntityManagerFactory(PersistenceUnit.DEFAULT);

    private void carregaBase(IDataSet estadoInicialDoBanco) throws DatabaseUnitException, ClassNotFoundException, SQLException {
        connection = new DatabaseConnection(PersistenceUnit.getConnection(PersistenceUnit.DEFAULT));

        DatabaseOperation.CLEAN_INSERT.execute(connection, estadoInicialDoBanco);
    }

    public static void main(String[] args) {

        Map<Integer, String> idMap = new HashMap<Integer, String>();

        generateIDs(idMap);

        CargaDeBanco exec = new CargaDeBanco();

        try {
            InputStream inStream = CargaDeBanco.class.getClassLoader().getResourceAsStream("cargaDeBanco.xml");

            IDataSet estadoInicialDoBanco = new FlatXmlDataSetBuilder().build(inStream);

            ReplacementDataSet rDataSet = new ReplacementDataSet(estadoInicialDoBanco);

            doNull(rDataSet);
            doGetNow(rDataSet);
            doGetRandomPast(rDataSet);
            doGetRandomFuture(rDataSet);
            doGeneratedId(rDataSet, idMap);

            exec.carregaBase(rDataSet);

        } catch (DatabaseUnitException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    private static void generateIDs(Map<Integer, String> idMap) {
        for (int id = 0; id < NUMERO_MAXIMO; id++) {
            generateID(id, idMap);
        }
    }

    private static void generateID(Integer key, Map<Integer, String> idMap) {
        UUID uuid = UUID.randomUUID();
        idMap.put(key, uuid.toString());
    }

    private static void doGeneratedId(ReplacementDataSet rDataSet, Map<Integer, String> idMap) {
        for (int i = 0; i < NUMERO_MAXIMO; i++) {
            rDataSet.addReplacementObject("[generated_id(" + i + ")]", idMap.get(i));
        }
    }

    private static void doNull(ReplacementDataSet rDataSet) {
        rDataSet.addReplacementObject("[null()]", null);
    }

    private static void doGetNow(ReplacementDataSet rDataSet) {
        rDataSet.addReplacementObject("[get_now()]", new Date());
    }

    private static void doGetRandomPast(ReplacementDataSet rDataSet) {
        for (int i = 0; i < NUMERO_MAXIMO; i++) {
            LocalDateTime now = new LocalDateTime();
            int randomDays = ((int) (Math.random() * 1000000)) % ((int) (QTD_DIAS_NO_ANO * now.getYear()));
            LocalDateTime randomPast = now.minusDays(randomDays);
            rDataSet.addReplacementObject("[get_random_past(" + i + ")]", randomPast.toString());
        }
    }

    private static void doGetRandomFuture(ReplacementDataSet rDataSet) {
        for (int i = 0; i < NUMERO_MAXIMO; i++) {
            LocalDateTime now = new LocalDateTime();
            int randomDays = ((int) (Math.random() * 1000000)) % ((int) (QTD_DIAS_NO_ANO * 60));//< no máximo 60 anos para o futuro
            LocalDateTime randomFuture = now.plusDays(randomDays);
            rDataSet.addReplacementObject("[get_random_future(" + i + ")]", randomFuture.toString());
        }
    }
}
